package vue;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import controller.PlateformeMain;

@SuppressWarnings("serial")
public class ModifyPluginPathFrame extends JFrame {

	private JPanel panel;
	
	public ModifyPluginPathFrame(){
		super();
		
		this.panel=new JPanel();
		
		this.setTitle("Plugins' path"); 
		this.setSize(540,170);
		this.setLocationRelativeTo(null); 
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		this.panel.setBackground(Color.white);
		this.panel.setLayout(new GridBagLayout());
		this.setContentPane(this.panel);
		
		this.buildContent();
		this.open();
	}

	private void buildContent() {
		boolean useConfig = PlateformeMain.core.getPluginStorage().getPluginPath().isUseConfigFile();
		
		ButtonGroup bg = new ButtonGroup();
		JRadioButton br1 = new JRadioButton("Utiliser un fichier de configuration : ");
		br1.setBackground(Color.white);
		br1.setSelected(useConfig);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.anchor=GridBagConstraints.WEST;
		
		final JTextField urlCFG = new JTextField();
		urlCFG.setName("urlCFG");
		urlCFG.setText(PlateformeMain.core.getPluginStorage().getPluginPath().getPluginsConfigPath());
		GridBagConstraints gbc3 = new GridBagConstraints();
		gbc3.gridx=1;
		gbc3.gridy=1;
		gbc3.weightx=1;
		gbc3.fill=GridBagConstraints.HORIZONTAL;
		gbc3.insets=new Insets(0, -30, 0, 0);
		this.panel.add(urlCFG,gbc3);
		
		JButton buttonCFG = new JButton("Parcourir...");
		buttonCFG.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
		        JFileChooser chooser = new JFileChooser();
		        FileFilter filter = new FileNameExtensionFilter("CFG File", "cfg");
		        chooser.addChoosableFileFilter(filter);
		        chooser.setFileHidingEnabled(true);
		        chooser.setAcceptAllFileFilterUsed(false);
		        int returnVal = chooser.showDialog(panel, "Choisir");
		        if(returnVal == JFileChooser.APPROVE_OPTION) {
		        	urlCFG.setText(chooser.getSelectedFile().getPath());
		        }
		    }
		});
		
		GridBagConstraints gbc4 = new GridBagConstraints();
		gbc4.gridx=2;
		gbc4.gridy=1;
		//gbc4.weightx=1;
		gbc4.fill=GridBagConstraints.HORIZONTAL;
		this.panel.add(buttonCFG,gbc4);
		
		
		JRadioButton br2 = new JRadioButton("Indiquer l'emplacement des plugins : ");
		br2.setBackground(Color.white);
		br2.setSelected(!useConfig);
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.gridx=0;
		gbc1.gridy=2;
		gbc1.anchor=GridBagConstraints.WEST;
		
		JTextField url = new JTextField();
		url.setName("url");
		url.setText("");
		GridBagConstraints gbc5 = new GridBagConstraints();
		gbc5.gridx=1;
		gbc5.gridy=3;
		gbc5.weightx=2;
		gbc5.insets=new Insets(0, -30, 0, 0);
		gbc5.fill=GridBagConstraints.HORIZONTAL;
		this.panel.add(url,gbc5);
		
		// ajout des boutons radio dans le groupe bg
		bg.add(br1);
		bg.add(br2);
		
		JButton save = new JButton("Enregistrer");
		GridBagConstraints gbc2 = new GridBagConstraints();
		gbc2.gridx=2;
		gbc2.gridy=4;
		gbc2.insets=new Insets(20,0,0,0);
		gbc2.anchor=GridBagConstraints.EAST;

		//save.addActionListener(new SavePluginPathListener());
		
		this.panel.add(br1,gbc);
		this.panel.add(br2,gbc1);
		this.panel.add(save,gbc2);
	}

	public void open() {
		this.setVisible(true);
	}
}
